package com.briup.jd2511.jd2511_product_source.controller;

import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerFenceHouse;
import com.briup.jd2511.jd2511_product_source.pojo.ext.ManagerFenceHouseExt;
import com.briup.jd2511.jd2511_product_source.result.Result;
import com.briup.jd2511.jd2511_product_source.service.FenceHouseService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Tag(name = "栏舍管理模块")
@RestController
@RequestMapping("/fenceHouse")
public class FenceHouseController {
    @Autowired
    private FenceHouseService fenceHouseService;

    @GetMapping
    @Operation(summary = "查询所有栏舍(含栏圈)")
    @Parameters(value = {
            @Parameter(name = "fhName",description = "栏舍名称"),
            @Parameter(name = "pageName",description = "页码"),
            @Parameter(name = "pageSize",description = "每页数量")
    })
    public Result getAllByConditionByPage(@RequestParam(required = false)String fhName,
                                          @RequestParam(defaultValue = "1")Integer pageName,
                                          @RequestParam(defaultValue = "10")Integer pageSize){
        PageInfo<ManagerFenceHouse> pageInfo = fenceHouseService.getAllFhNameByPage(fhName,pageName,pageSize);
        return Result.success(pageInfo);
    }

    @GetMapping("/{fhId}")
    @Operation(summary = "查询指定栏舍(含栏圈)")
    @Parameter(name = "fhId",description = "栏舍id", in = ParameterIn.PATH)
    public Result getByIdWithMhs(@PathVariable String fhId){
        ManagerFenceHouseExt fenceHouseExt = fenceHouseService.getDetailByFhId(fhId);
        return Result.success(fenceHouseExt);
    }

    @PostMapping("/saveOrUpdate")
    @Operation(summary = "新增/修改栏舍", description = "请求体含fhId则修改，不含则新增；栏舍名称需唯一")
    @Parameters(value = {
            @Parameter(name = "fhId",description = "栏舍id（修改时必传，新增时不传）"),
            @Parameter(name = "fhName",description = "栏舍名称（必传，且唯一）"),
            @Parameter(name = "fhDesc",description = "栏舍描述"),
            @Parameter(name = "fhTime",description = "栏舍创建时间")
    })
    public Result saveOrUpdate(@RequestBody ManagerFenceHouse fenceHouse){
        Result result = fenceHouseService.saveOrUpdate(fenceHouse);
        return result;
    }

    @DeleteMapping("/{fhId}")
    @Operation(summary = "根据fhId删除栏舍")
    @Parameter(name = "fhId",description = "栏舍id", in = ParameterIn.PATH)
    public Result deleteById(@PathVariable String fhId){
        try{
            fenceHouseService.deleteById(fhId);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }
    }

    @DeleteMapping("/deleteByIdAll")
    @Operation(summary = "批量删除栏舍")
    @Parameter(name = "fhIds",description = "栏舍id数组", in = ParameterIn.QUERY)
    public Result deleteByIdAll(@RequestBody List<String> fhIds){
        try {
            fenceHouseService.deleteByIdAll(fhIds);
            return Result.success();
        }catch (ServiceException e){
            return Result.failure(e.getResultCode());
        }
    }

    @GetMapping("/queryAll")
    @Operation(summary = "查询所有栏舍")
    public Result queryAll(){
        return Result.success(fenceHouseService.queryAll());
    }


}